package com.huawei.tree.binary_tree_array;

//顺序存储二叉树遍历
public class BinaryTreeArray {

    //存储数据节点的数组
    private String[] array;

    public BinaryTreeArray(String[] array) {
        this.array = array;
    }

    /**
     * @Name: 顺序存储二叉树的前序遍历
     * @Author: GaoJian
     * @Date: 2022/4/17 02:04
     * @Description:
     */
    public void preOrder(int index) {
        //1、判断当前数组是否为空
        if (array == null || array.length == 0) {
            System.out.println("数组为空，不能遍历");
        }
        //2、输出当前节点
        System.out.print(array[index] + "、");
        //3、向左递归遍历
        if (2 * index + 1 < array.length) {
            preOrder(2 * index + 1);
        }
        //4、向右递归遍历
        if (2 * index + 2 <array.length) {
            preOrder(2 * index + 2);
        }
    }

    public void preOrder() {
        preOrder(0);
    }

    /**
     * @Name: 顺序存储二叉树的中序遍历
     * @Author: GaoJian
     * @Date: 2022/4/17 02:06
     * @Description:
     */
    public void midOrder(int index) {
        //1、判断当前数组是否为空
        if (array == null || array.length == 0) {
            System.out.println("数组为空，不能遍历");
        }
        //2、向左递归遍历
        if (2 * index + 1 < array.length) {
            midOrder(2 * index + 1);
        }
        //3、输出当前节点
        System.out.print(array[index] + "、");
        //4、向右递归遍历
        if (2 * index + 2 <array.length) {
            midOrder(2 * index + 2);
        }
    }

    public void midOrder() {
        midOrder(0);
    }

    /**
     * @Name: 顺序存储二叉树的后序遍历
     * @Author: GaoJian
     * @Date: 2022/4/17 02:06
     * @Description:
     */
    public void afterOrder(int index) {
        //1、判断当前数组是否为空
        if (array == null || array.length == 0) {
            System.out.println("数组为空，不能遍历");
        }
        //2、向左递归遍历
        if (2 * index + 1 < array.length) {
            afterOrder(2 * index + 1);
        }
        //3、向右递归遍历
        if (2 * index + 2 <array.length) {
            afterOrder(2 * index + 2);
        }
        //4、输出当前节点
        System.out.print(array[index] + "、");
    }

    public void afterOrder() {
        afterOrder(0);
    }

}
